<!-- 弹窗 详情 -->
<script lang="tsx" setup>
import FileUpload from "@/components/file-system/file-upload.vue";
import type { FormDescChild } from "@/components/form-desc/form-desc.vue";
import { enclosureParkBarriersSaveOrUpdateUsingPost } from "@/genapi/wh-yqd";
import { useAsync } from "@/hooks/use-async";
import FileOtherUpload from "@/components/file-system/file-otherUpload.vue";
import { ref } from "vue";
import {
	getEnumOptionsField,
	GATE_STATUS_OPTIONS,
	RECORD_TYPE_OPTIONS,
} from "@/config/enum-options";

import { injectState } from "../state";
import { useUserStore } from "@/stores/user";

const userStore = useUserStore();

defineOptions({ name: "DetailDialog" });

const img = import.meta.glob("./assets/*.png", {
	import: "default",
	eager: true,
});

const { page } = injectState()!;

const dialogRef = shallowRef<InstanceType<GlobalComponents["PromiseDialog"]>>();
const formDescRef = shallowRef<InstanceType<GlobalComponents["FormDesc"]>>();

const type = ref<"write" | "read">();

const beforeClose = async () => {
	if (type.value === "write") {
		await ElMessageBox.confirm("确认关闭？");
	}
};

const trigger = async (_type: "write" | "read" = "read", value = {} as any) => {
	type.value = _type;
	model.value = JSON.parse(JSON.stringify(value)) as any;

	await dialogRef.value?.open();
};

defineExpose({
	trigger,
});

const model = ref<(typeof page.records.value)[0]>({});
const value = ref("");

const file = computed({
	get() {
		if (model.value.fileId) {
			return [
				{
					id: model.value.fileId,
					name: model.value.fileName,
				},
			];
		}
	},
	set(value: any) {
		model.value.fileId = value?.[0]?.id;
	},
});

const title = computed(() => {
	if (type.value === "write") {
		return model.value.id ? "编辑" : "新建";
	} else {
		return `详情`;
	}
});

const { execute, isLoading } = useAsync(async () => {
	await formDescRef.value?.formRef?.validate();
	await ElMessageBox.confirm("确认提交？", "提示", {
		type: "warning",
	});

	await enclosureParkBarriersSaveOrUpdateUsingPost({
		data: {
			...model.value,
		},
	});
	ElMessage.success("提交成功");
	page.execute();
	dialogRef.value?.complete();
});
const selectOptions = ref([{ label: "员工", value: "1" }]);

function getCurrentDate(): string {
	const now = new Date();
	const year = now.getFullYear();
	const month = String(now.getMonth() + 1).padStart(2, "0");
	const day = String(now.getDate()).padStart(2, "0");

	return `${year}-${month}-${day}`;
}
model.value.time = getCurrentDate();
const children1: FormDescChild[] = [
	{
		label: "所属单位",
		prop: "orgCode",
		rule: [{ required: false, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.orgCode} placeholder="请输入" />
		),
		descItem: () => `${model.value.orgCode ?? "-"}`,
	},
	{
		label: "姓名",
		prop: "name",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.name} placeholder="请输入" />
		),
		descItem: () => `${model.value.name ?? "-"}`,
	},
	{
		label: "身份证号",
		prop: "icNumber",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.icNumber} placeholder="请输入" />
		),
		descItem: () => `${model.value.icNumber ?? "-"}`,
	},
	{
		label: "联系电话（用户账号）",
		prop: "phone",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.phone} placeholder="请输入" />
		),
		descItem: () => `${model.value.phone ?? "-"}`,
	},
	{
		label: "从业资格证号",
		prop: "credentials",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.credentials} placeholder="请输入" />
		),
		descItem: () => `${model.value.credentials ?? "-"}`,
	},
	{
		label: "从业资格证有效期",
		prop: "creEndDate",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-date-picker
				v-model={model.value.creEndDate}
				clearable
				type="date"
				placeholder="安装时间"
				value-format="YYYY-MM-DD"
			/>
		),
		descItem: () => `${model.value.creEndDate ?? "-"}`,
	},
	{
		label: "工种类别",
		prop: "workCategoryText",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.workCategoryText} placeholder="请输入" />
		),
		descItem: () => `${model.value.workCategoryText ?? "-"}`,
	},
	{
		label: "车辆类型",
		prop: "vehicleType",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.vehicleType} placeholder="请输入" />
		),
		descItem: () => `${model.value.vehicleType ?? "-"}`,
	},
];

const children2: FormDescChild[] = [
	{
		label: "从业资格证",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 2,
		formItem: () => (
			<div style={{ marginTop: "10px", marginLeft: "15px" }}>
				<FileOtherUpload
					onClick={() => {
						userStore.imgType = "从业资格证";
					}}
				/>
				<div
					style={{
						whiteSpace: "normal",
						width: "150px",
						color: "rgba(0, 0, 0, 0.25)",
					}}
				>
					支持bmp,jpg,png,tif格式图片可添加多个图片
				</div>
			</div>
		),
		descItem: () => `${model.value.park ?? "-"}`,
	},
];

const children3: FormDescChild[] = [
	{
		label: "培训类型",
		prop: "workCategoryText",
		rule: [{ required: false, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.workCategoryText} placeholder="请输入" />
		),
		descItem: () => `${model.value.workCategoryText ?? "-"}`,
	},
	{
		label: "报名时间",
		prop: "creStartDate",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-date-picker
				v-model={model.value.creStartDate}
				clearable
				type="date"
				placeholder="报名时间"
				value-format="YYYY-MM-DD"
			/>
		),
		descItem: () => `${model.value.creStartDate ?? "-"}`,
	},
	{
		label: "审核通过时间",
		prop: "creStartDate",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-date-picker
				v-model={model.value.creStartDate}
				clearable
				type="date"
				placeholder="审核通过时间"
				value-format="YYYY-MM-DD"
			/>
		),
		descItem: () => `${model.value.creStartDate ?? "-"}`,
	},
	{
		label: "总学分",
		prop: "name",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.name} placeholder="请输入" />
		),
		descItem: () => `${model.value.name ?? "-"}`,
	},
	{
		label: "培训总时长",
		prop: "number",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.number} placeholder="请输入" />
		),
		descItem: () => `${model.value.number ?? "-"}`,
	},
	{
		label: "培训课程",
		prop: "location",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.location} placeholder="请输入" />
		),
		descItem: () => `${model.value.location ?? "-"}`,
	},
	{
		label: "所得学分",
		prop: "coordinate",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.coordinate} placeholder="请输入" />
		),
		descItem: () => `${model.value.coordinate ?? "-"}`,
	},
	{
		label: "培训状态",
		prop: "status",
		rule: [{ required: false, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-select-v2
				v-model={model.value.status}
				options={GATE_STATUS_OPTIONS}
				clearable
				placeholder="请选择类型"
			/>
		),
		descItem: () =>
			getEnumOptionsField(GATE_STATUS_OPTIONS, "label", model.value.status) ??
			"-",
	},
];

const children4: FormDescChild[] = [
	{
		label: "考试时间",
		prop: "applyDate",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-date-picker
				v-model={model.value.applyDate}
				clearable
				type="date"
				placeholder="考试时间"
				value-format="YYYY-MM-DD"
			/>
		),
		descItem: () => `${model.value.applyDate ?? "-"}`,
	},
	{
		label: "结束时间",
		prop: "applyDate",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-date-picker
				v-model={model.value.applyDate}
				clearable
				type="date"
				placeholder="结束时间"
				value-format="YYYY-MM-DD"
			/>
		),
		descItem: () => `${model.value.applyDate ?? "-"}`,
	},
	{
		label: "试卷名称",
		prop: "name",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.name} placeholder="请输入" />
		),
		descItem: () => `${model.value.name ?? "-"}`,
	},
	{
		label: "考试场次",
		prop: "name",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.name} placeholder="请输入" />
		),
		descItem: () => `${model.value.name ?? "-"}`,
	},
	{
		label: "总分",
		prop: "name",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.name} placeholder="请输入" style='color: red;'/>
		),
		descItem: () => `${model.value.name ?? "-"}`,
	},
	{
		label: "得分",
		prop: "name",
		rule: [{ required: true, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-input v-model={model.value.name} placeholder="请输入" />
		),
		descItem: () => `${model.value.name ?? "-"}`,
	},
	{
		label: "考试状态",
		prop: "status",
		rule: [{ required: false, message: "请输入", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<el-select-v2
				v-model={model.value.status}
				options={GATE_STATUS_OPTIONS}
				clearable
				placeholder="请选择类型"
			/>
		),
		descItem: () =>
			getEnumOptionsField(GATE_STATUS_OPTIONS, "label", model.value.status) ??
			"-",
	},
];

interface Option {
	key: number;
	label: string;
	initial: string;
}

const generateData = () => {
	const data: Option[] = [];
	const states = [
		"California",
		"Illinois",
		"Maryland",
		"Texas",
		"Florida",
		"Colorado",
		"Connecticut ",
	];
	const initials = ["CA", "IL", "MD", "TX", "FL", "CO", "CT"];
	states.forEach((city, index) => {
		data.push({
			label: city,
			key: index,
			initial: initials[index],
		});
	});
	return data;
};

const data = ref<Option[]>(generateData());
const value1 = ref([]);

const filterMethod = (query, item) => {
	return item.initial.toLowerCase().includes(query.toLowerCase());
};
</script>

<template>
	<PromiseDialog
		ref="dialogRef"
		:title="title"
		:before-close="beforeClose"
		w="700px!"
	>
		<el-scrollbar style="height: 508px">
			<span class="childTitle" style="color: rgb(48, 49, 51); font-weight: 800; font-size: 16px;">基本信息</span>
			<FormDesc
				ref="formDescRef"
				m="t-24px x-30px"
				:type="type === 'read' ? 'desc' : 'form'"
				form-class="gap-x-20px"
				:model="model"
				:children="children1"
				:column="2"
			/>
			<span class="childTitle" style="color: rgb(48, 49, 51); font-weight: 800; font-size: 16px;">资质照片</span>
			<FormDesc
				ref="formDescRef"
				m="t-24px x-30px"
				:type="type === 'read' ? 'desc' : 'form'"
				form-class="gap-x-20px"
				:model="model"
				:children="children2"
				:column="2"
			/>
			<span class="childTitle" style="color: rgb(48, 49, 51); font-weight: 800; font-size: 16px;">培训信息</span>
			<FormDesc
				ref="formDescRef"
				m="t-24px x-30px"
				:type="type === 'read' ? 'desc' : 'form'"
				form-class="gap-x-20px"
				:model="model"
				:children="children3"
				:column="2"
			/>
			<span class="childTitle" style="color: rgb(48, 49, 51); font-weight: 800; font-size: 16px;">考试信息</span>
			<FormDesc
				ref="formDescRef"
				m="t-24px x-30px"
				:type="type === 'read' ? 'desc' : 'form'"
				form-class="gap-x-20px"
				:model="model"
				:children="children4"
				:column="2"
			/>
		</el-scrollbar>
		<!-- <template v-if="type === 'write'" #footer>
			<el-button @click="dialogRef?.close()">取消</el-button>
			<el-button type="primary" :is-loading="isLoading" @click="execute()"
				>确定</el-button
			>
		</template>
		<template v-else-if="type === 'read'" #footer>
			<el-button @click="dialogRef?.close()">拒绝</el-button>
			<el-button type="primary" :is-loading="isLoading" @click="execute()"
				>通过</el-button
			>
		</template> -->
	</PromiseDialog>
</template>

<style lang="scss" scoped>
.trainingListBox {
	width: 100%;
	padding: 0px 30px;
	.listBoxTitle {
		height: 50px;
		width: 100%;
		display: flex;
		align-items: center;
	}
}
</style>
